import juice from "juice";
import {
  BASIC_THEME_ID,
  CODE_THEME_ID,
  MARKDOWN_THEME_ID,
  LAYOUT_ID,
  BOX_ID,
  FONT_THEME_ID,
  PREVIEW_CLASS
} from "./constant";

// 转换微信公众号代码
export const solveWeChatMath = () => {
  // const layout = document.getElementById(LAYOUT_ID);
  const mjxs = document.getElementsByClassName(PREVIEW_CLASS)[0];

  for (let i = 0; i < mjxs.length; i++) {
    const mjx = mjxs[i];
    if (!mjx.hasAttribute("jax")) {
      break;
    }

    // mjx.removeAttribute("data");
    mjx.removeAttribute("jax");
    mjx.removeAttribute("display");
    mjx.removeAttribute("tabindex");
    mjx.removeAttribute("ctxtmenu_counter");
    const svg = mjx.firstChild;
    const width = svg.getAttribute("width");
    const height = svg.getAttribute("height");
    svg.removeAttribute("width");
    svg.removeAttribute("height");
    svg.style.width = width;
    svg.style.height = height;
  }
};

// html和css合并
export const solveHtml = () => {
  let preClass = document.getElementsByClassName(PREVIEW_CLASS)[0];
  let niceId = preClass.innerHTML.includes('nice');
  let html = preClass.innerHTML;
  let html1 = '';
  if (niceId) {
    html1 = html;
  } else {
    html1 =  '<section id="nice" >' + html + '</section>'
  }

  // let html = element.innerHTML;

  // html = html.replace(/<mjx-container (class="inline.+?)<\/mjx-container>/g, "<span $1</span>");
  // html = html.replace(/\s<span class="inline/g, '&nbsp;<span class="inline');
  // html = html.replace(/svg><\/span>\s/g, "svg></span>&nbsp;");
  // html = html.replace(/mjx-container/g, "section");
  // html = html.replace(/class="mjx-solid"/g, 'fill="none" stroke-width="70"');
  // html = html.replace(/<mjx-assistive-mml.+?<\/mjx-assistive-mml>/g, "");

  // const basicStyle = document.getElementById(BASIC_THEME_ID)?.innerText || '';
  // const markdownStyle = document.getElementById(MARKDOWN_THEME_ID)?.innerText || '';
  // const codeStyle = document.getElementById(CODE_THEME_ID)?.innerText || '';
  // const fontStyle = document.getElementById(FONT_THEME_ID)?.innerText || '';

  let myStyle = document.getElementById('myStyleId');
  let css = '';
  if (myStyle) {
    css = myStyle.innerHTML;
  }
  let basicStyle = document.getElementById('basic').innerText || '';


  let res = "";
  try {
    res = juice.inlineContent(html1, basicStyle + css, {
      inlinePseudoElements: true,
      preserveImportant: true,
    });
  } catch (e) {
    // message.error("请检查 CSS 文件是否编写正确！");
  }
  // console.log('res = ',res);
  return res;
};

export const copySafari = (text) => {
  // 获取 input
  let input = document.getElementById("copy-input");
  if (!input) {
    // input 不能用 CSS 隐藏，必须在页面内存在。
    input = document.createElement("input");
    input.id = "copy-input";
    input.style.position = "fixed"; // 1
    input.style.left = "-1000px";
    input.style.top = "0px"; // 2    1,2修复了复制后页面滚动问题
    input.style.zIndex = "-1000";
    document.body.appendChild(input);
  }
  // 让 input 选中一个字符，无所谓那个字符
  input.value = "NOTHING";
  input.setSelectionRange(0, 1);
  input.focus();

  // 复制触发
  document.addEventListener("copy", function copyCall(e) {
    e.preventDefault();
    e.clipboardData.setData("text/html", text);
    e.clipboardData.setData("text/plain", text);
    document.removeEventListener("copy", copyCall);
  });
  document.execCommand("copy");
};
